package com.niuxiaofa.newlaw.Tools;

/**
 * Created by Gxjkz on 2017/8/28.
 */

import android.util.Log;

import com.bin.david.form.data.form.IForm;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UUDateUtils extends Date {

    public static String getTodayTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getTodayTime2() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getTodayTimeDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getTodayTimeDate4() {
        SimpleDateFormat df = new SimpleDateFormat("MM.dd");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    //获取时间戳
    public static long getUnixTimestamp() {
        return System.currentTimeMillis();
    }


    public static String interceptTime(String a) {
        String c = "";
        try {
            c = a.replace("T", " ").split(" ")[0];
        } catch (Exception e) {

        }
        return c;
    }

    public static String interceptTime2(String a) {
        String c = "";
        try {
            c = a.substring(0,9).replace("-",".");


        } catch (Exception e) {

        }
        return c;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    /**
     * 获取明天的日期字符串
     *
     * @return
     */
    public static String tomorrowDateStr(int day) {
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //把日期往后增加一天.整数往后推,负数往前移动(1:表示明天、-1：表示昨天，0：表示今天)
        calendar.add(Calendar.DATE, day);
        //这个时间就是日期往后推一天的结果
        date = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("MM.dd");
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }



    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        if (s != null) {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            long lt = new Long(s);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            return res;
        } else {
            return "";
        }
    }


    public static String stringToDate(Long lo) {
        if (lo == null || lo == 0) {
            return "";
        }
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }

    public static String stringToDate2(Long lo) {
        if (lo == null || lo == 0) {
            return "";
        }
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd");
        return sd.format(date);
    }

    public static String stringToDate3(Long lo) {
        if (lo == null || lo == 0) {
            return "";
        }
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        return sd.format(date);
    }


    public static String secondToDate(int lo) {
        int h = lo / 3600;
        int m = (lo % 3600) / 60;
        int s = (lo % 3600) % 60;
        System.out.println(h + "时" + m + "分" + s + "秒");

        return h + ":" + m + ":" + s;
    }


//
//    public static String dateToString(String s) throws ParseException {
//        String res;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date = simpleDateFormat.parse(s);
//        long ts = date.getTime();
//        res = String.valueOf(ts);
//        return res;
//    }


    public static String dateToString(Date data) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(data);//格式化数据
        return date;
    }


    //当月第一天
    public static String getMothonOneDayTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String first = df.format(c.getTime());
        Log.e("MothonOneDayTime: ", first);
        return first;// new Date()为获取当前系统时间
    }

    //当月最后一天
    public static String getMothonEndDayTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = df.format(ca.getTime());
        Log.e("MothonEndDayTime: ", last);
        return last;// new Date()为获取当前系统时间
    }

    public String lastMonthFrist() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM- HH:mm:ss");//设置日期格式
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String firstDay = df.format(cal_1.getTime());
        Log.e("firstDay:", firstDay);
        return firstDay;
    }

    public String lastMonthEnd() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM- HH:mm:ss");//设置日期格式
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        String lastDay = df.format(cale.getTime());
        System.out.println("-----2------lastDay:" + lastDay);
        return lastDay;
    }


    /**
     * 指定月有几天
     *
     * @param year
     * @param month
     * @return
     */
    public int getcountDaysInMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        return dayOfMonth;
    }


    //获取前一天日期  time正为提前 负为前
    public static Date getNextDay(Date date, int time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, time);
        date = calendar.getTime();
        return date;
    }

    //获取当前系统前一天日期  time正为提前 负为前
    public static Date getNextMonth(Date date, int time) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, time);
        date = calendar.getTime();
        return date;
    }

    //获取当前系统前一天日期  time正为提前 负为前
    public static Date getNextHOUR(Date date, int time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, time);
        date = calendar.getTime();
        return date;
    }


    //时间比对
    public static int compare_date(String DATE1, String DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                //   Log.e("dt1 在dt2前",dt1.toString()+" "+dt2.toString());
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                //      Log.e("dt1在dt2后",dt1.toString()+" "+dt2.toString());
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            Log.e("", exception.toString());
        }
        return 0;
    }


    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //不同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //同一年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }
}